/**
 * Author: 何文飞
 * date: 2023-04-22 17:18:59
 * description: 接口文档
 */
import main from '@/store/index'
import type { create_coupons } from '@/types/coupons'
import axios from 'axios'
import { storeToRefs } from 'pinia'
import Request from './request'
const { cancel } = storeToRefs(main())
const request = new Request({
    // baseURL: 'https://api.chatai.gd.cn/',
    baseURL: "https://api.qiuzhishe.club/",
    timeout: 1000 * 60 * 10
})
const CancelToken = axios.CancelToken

type result = {
    result?: any
    message: string
    code: number
}

export const create_question = (params: { content: string, chat_id: string }) => {
    return request.post<result>({ url: '/questions', data: { ...params, gpt_model: "gpt-3.5-turbo" } })
}
export const ask = (params: { prompt: string; chat_id: string }) => {
    return request.get<result>({
        url: '/completions',
        params,
        cancelToken: new CancelToken(function executor(c) {
            // executor 函数接收一个 cancel 函数作为参数
            cancel.value = c
        })
    })
}
export const complete_question = (params: { content: string, id: number | null }) => {
    return request.post<result>({ url: `/questions/${params.id}/complete_response`, data: params })
}
export const start_response = (params: { id: number | null }) => {
    return request.post<result>({ url: `/questions/${params.id}/start_response` })
}


export const ask_loop = (params: { index: number; uuid: string }) => {
    return request.get<result>({
        url: `/questions/${params.uuid}`,
        params: { index: params.index },
        cancelToken: new CancelToken(function executor(c) {
            // executor 函数接收一个 cancel 函数作为参数
            cancel.value = c
        })
    })
}



export const login = (params: { phone: string; password: string }) => {
    return request.post<result>({ url: '/login', data: params })
}
export const register = (params: { phone: string; password: string; verify_code: string; name: string }) => {
    return request.post<result>({ url: '/users', data: params })
}
export const send_reg_verify_code = (params: { phone: string }) => {  //注册验证码
    return request.post<result>({ url: '/sms/send_reg_verify_code', data: params })
}
export const send_forget_pwd_verify_code = (params: { phone: string }) => {  //忘记密码验证码
    return request.post<result>({ url: '/sms/send_forget_pwd_verify_code ', data: params })
}
export const reset_pwd = (params: { phone: string; password: string; verify_code: string; }) => {
    return request.post<result>({ url: '/users/reset_pwd', data: params })
}
export const count_stop = (params: { id: number | null, content: string }) => {  //问题中止时统计
    return request.post<result>({ url: `/questions/${params.id}/stop_response`, data: params })
}

// 窗口信息
export const get_chats = () => {
    return request.get<result>({ url: `/chats` })
}
export const get_chats_detail = (id: string) => {
    return request.get<result>({ url: `/chats/${id}` })
}
export const delete_chats = (id: number) => {
    return request.delete<result>({ url: `/chats/${id}` })
}
export const update_chats = (params: { title: string; id: number }) => {
    return request.put<result>({ url: `/chats/${params.id}`, data: params })
}
export const clear_coversations = () => {
    return request.post<result>({ url: '/chats/clear_coversations' })
}
export const create_chats = () => {
    return request.post<result>({ url: '/chats' })
}

//数据使用情况
export const tokens = (month: number) => {
    return request.get<result>({ url: `/tokens`, params: { month } })
}

//---------后台管理
// 每日活跃数
export const dau = (params: { year: number; month: number }) => {
    return request.get<result>({ url: `/statistics/dau`, params })
}
// 每日注册数
export const dru = (params: { year: number; month: number }) => {
    return request.get<result>({ url: `/statistics/dru`, params })
}
// 用户管理
export const users = (params: { name: string; phone: string }) => {
    return request.get<result>({ url: `/users`, params })
}
// 问题管理
export const questions = (params: { content: string; user_id: number }) => {
    return request.get<result>({ url: `/questions`, params })
}
// 问题详细
export const questionsDetail = (id: string) => {
    return request.get<result>({ url: `/questions/${id}/detail` })
}
// 优惠券管理
export const coupons = (params: any) => {
    return request.get<result>({ url: `/coupons`, params })
}
// 创建优惠券
export const createCoupons = (data: create_coupons) => {
    return request.post<result>({ url: '/coupons', data })
}
//使用优惠券
export const useCoupons = (coupon_code: string) => {
    return request.post<result>({ url: '/transactions', data: { coupon_code } })
}
export const sessions = () => {
    return request.get<result>({ url: `/sessions` })
}

// 图片识别
export const ocr = (formData: FormData) => {
    return request.post<result>({ url: '/ocr', data: formData, headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
}
export const updateImg = (params: { formData: FormData; fileName: string }) => {
    return request.put<result>({ url: `https://chatai-center.oss-cn-guangzhou.aliyuncs.com/${params.fileName}`, data: params.formData })
}


// 获取tag
export const getTags = () => {
    return request.get<result>({ url: `/tags` })
}
export const prompts = (params: { tags: Number[] }) => {
    return request.get<result>({ url: `/prompts`, params })
}
export const getPrompts = () => {
    return request.get<result>({ url: `/prompts/list` })
}
export const collectPrompts = (params: { id: number }) => {
    return request.put<result>({ url: `/prompts/${params.id}/collect`, data: params })
}
export const uncollectPrompts = (params: { id: number }) => {
    return request.put<result>({ url: `/prompts/${params.id}/uncollect`, data: params })
}

// 获取产品
export const getProduct = (params: any) => {
    return request.get<result>({ url: `/products`, params })
}

//下单
export const chatgptTrades = (data: { product_id: number }) => {
    return request.post<result>({ url: '/chatgpt_trades', data })
}

//获取订单信息
export const getTradesInfo = (params: { code: string }) => {
    return request.get<result>({ url: `/chatgpt_trades/${params.code}` })
}

// 获取当前订单的支付状态
export const getPaystatus = (params: { trade_no: string }) => {
    return request.get<result>({ url: `/payments/done`, params })
}